package com.zhouchao.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.zhouchao.bean.Score;
/**
 * 卫生评比
 * 
 * @author zhouchao
 *
 */
public class ScoreDao {
	//列出全部
	 
	public List<Score> scoreList(Connection con)throws Exception {
		List<Score> scorelist = new ArrayList<Score>();
		String sql = "select * from t_score order by id desc";
		PreparedStatement pstmt = con.prepareStatement(sql);
		ResultSet rs = pstmt.executeQuery();
		while(rs.next()) {
			Score info=new Score();
			info.setDormBuildId(rs.getString("dormBuildId"));
			info.setDormName(rs.getString("dormName"));
			info.setDate(rs.getString("date"));
			info.setWeek(rs.getInt("week"));
			info.setResult(rs.getFloat("result"));
			
			scorelist.add(info);
		}
		return scorelist;
	}
	
	//列出本周
	public  List<Score> scoreList(Connection con,int week)throws Exception {
		List<Score> scorelist = new ArrayList<Score>();
		String sql = "select * from t_score where week="+week+" order by result desc";
		PreparedStatement pstmt = con.prepareStatement(sql);
		ResultSet rs = pstmt.executeQuery();
		while(rs.next()) {
			Score info=new Score();
			info.setDormBuildId(rs.getString("dormBuildId"));
			info.setDormName(rs.getString("dormName"));
			info.setDate(rs.getString("date"));
			info.setWeek(rs.getInt("week"));
			info.setResult(rs.getFloat("result"));
			
			scorelist.add(info);
		}
		return scorelist;
	}
	//列出本周指定宿舍楼
	public List<Score> scoreList(Connection con,int week,String dormBuildId)throws Exception {
		List<Score> scorelist = new ArrayList<Score>();
		String sql = "select * from t_score where week="+week+" order by result desc";
		PreparedStatement pstmt = con.prepareStatement(sql);
		ResultSet rs = pstmt.executeQuery();
		while(rs.next()) {
			Score info=new Score();
			info.setDormBuildId(rs.getString("dormBuildId"));
			info.setDormName(rs.getString("dormName"));
			info.setDate(rs.getString("date"));
			info.setWeek(rs.getInt("week"));
			info.setResult(rs.getFloat("result"));
			
			scorelist.add(info);
		}
		return scorelist;
	}
	//当前寝室
	public List<Score> dormscore(Connection con,String dorm)throws Exception {
		List<Score> scorelist = new ArrayList<Score>();
		String sql = "select * from t_score where dormName='"+dorm+"' order by id desc";
		PreparedStatement pstmt = con.prepareStatement(sql);
		ResultSet rs = pstmt.executeQuery();
		while(rs.next()) {
			Score info=new Score();
			info.setDormBuildId(rs.getString("dormBuildId"));
			info.setDormName(rs.getString("dormName"));
			info.setDate(rs.getString("date"));
			info.setWeek(rs.getInt("week"));
			info.setResult(rs.getFloat("result"));
			
			scorelist.add(info);
		}
		return scorelist;
	}
	//登记
	public int intoscore(Connection con,Score score) throws Exception{
		String sql = "insert into t_score values(null,?,?,?,?,?)";
		PreparedStatement pstmt = con.prepareStatement(sql);
		pstmt.setString(1, score.getDormBuildId());
		pstmt.setString(2, score.getDormName());
		pstmt.setString(3, score.getDate());
		pstmt.setInt(4, score.getWeek());
		pstmt.setFloat(5, score.getResult());
		
		return pstmt.executeUpdate();
	}
	
}
